package com.landinfo.web.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.codahale.metrics.annotation.Timed;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.landinfo.service.GrassAuthenticRightService;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;

/**
 * 草原确权统计
 */
@RestController
@RequestMapping("/api/grass-authentic-right")
public class GrassAuthenticRightResource {

    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    //    @Autowired
    private GrassAuthenticRightService grassAuthenticRightService;

    public GrassAuthenticRightResource(GrassAuthenticRightService grassAuthenticRightService) {
        this.grassAuthenticRightService = grassAuthenticRightService;

    }

    /**
     * 根据类型获取历史记录
     *
     * @return
     */
    @PostMapping(value = "/history")
    @Timed
    public String getHistory(@RequestBody String param) {
        HashMap<String, Object> hashMap = new HashMap<>();

        try {
            JSONObject jsonObject = JSON.parseObject(param);
            String type = jsonObject.getString("type");
            String userName = jsonObject.getString("userName");
            List list = this.grassAuthenticRightService.getHistory(type, userName);
            hashMap.put("success", true);
            hashMap.put("rows", list);
        } catch (Exception ex) {
            hashMap.put("success", false);
            hashMap.put("message", ex.getLocalizedMessage());
        }


        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        String result = gson.toJson(hashMap);
        return result;

    }

    /**
     * 根据类型获取历史记录
     *
     * @return
     */
    @PostMapping("/query")
    @Timed
    public String Query(@RequestParam String param, @RequestParam String geom) {
        HashMap<String, Object> hashMap = new HashMap<>();

        try {
            JSONObject jsonObject = JSON.parseObject(param);
            String type = jsonObject.getString("type");
            String where = jsonObject.getString("where");
            hashMap = this.grassAuthenticRightService.Query(type, where, geom);

        } catch (Exception ex) {
            hashMap.put("success", false);
            hashMap.put("message", ex.getLocalizedMessage());
        }

        Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
        String result = gson.toJson(hashMap);
        return result;

    }


    /**
     * 草场承包责任制落实情况汇总表
     *
     * @param type
     * @return
     */
    @GetMapping("/getImplementationOfTheSituation/{type}/{xzqhCode}")
    @Timed
    public String getImplementationOfTheSituation(@PathVariable String type, @PathVariable String xzqhCode) {

        return this.grassAuthenticRightService.getImplementationOfTheSituation(type, xzqhCode);
    }

    /**
     * 根据parentId删除相关表数据
     *
     * @param parentId
     * @return
     */
    @DeleteMapping("/deleteByParentId/{templateType}/{parentId}")
    @Timed
    public String deleteByParentId(@PathVariable String templateType, @PathVariable String parentId) {

        return null;
    }


    /**
     * 草原承包情况汇总表
     * 1.村草原承包统计表
     * 2.全乡草原承包汇总表
     *
     * @param type
     * @return
     */
    @GetMapping("/getGrassContractedTotal/{type}/{village}")
    @Timed
    public String getGrassContractedTotal(@PathVariable String type, @PathVariable String village) {


        return this.grassAuthenticRightService.getGrassContractedTotal(type, village);
    }

    /**
     * 所有证统计
     *
     * @param type
     * @return
     */
    @GetMapping("/getAllEvidenceTotal/{type}")
    @Timed
    public String getAllEvidenceTotal(@PathVariable String type) {

        return this.grassAuthenticRightService.getAllEvidenceTotal(type);
    }


    /**
     * 使用证统计
     *
     * @param type
     * @return
     */
    @GetMapping("/getUsePermitTotal/{type}")
    @Timed
    public String getUsePermitTotal(@PathVariable String type) {

        return this.grassAuthenticRightService.getUsePermitTotal(type);
    }


    /**
     * 以下三个为一组
     * 户证件统计
     * 联户证统计
     * 村组统计
     *
     * @param paras
     * @return
     */
    @PostMapping("/getJointHouseholdsTotal")
    @Timed
    public String getJointHouseholdsTotal(@RequestParam String paras) {
        if (paras == null || "".equals(paras)) {
            return null;
        }
        HashMap<String, Object> result = new HashMap<>();
        String type = null;
        Double gxb = null;           // 干鲜比
        Double xcmcl = null;         // 鲜草亩产量
        String village = null;
        try {
            JSONObject jsonObject = JSON.parseObject(paras);
            type = jsonObject.getString("type");
            gxb = jsonObject.getDouble("gxb");
            xcmcl = jsonObject.getDouble("xcmcl");
            village = jsonObject.getString("village");

        } catch (Exception ex) {
            result.put("success", false);
            result.put("rows", null);
            return JSON.toJSONString(result);
        }
        result.put("success", true);
        result.put("rows", this.grassAuthenticRightService.getJointHouseholdsTotal(type, village, gxb, xcmcl));
        return JSON.toJSONString(result);
    }

    /**
     * 草场承包基本情况调查表
     *
     * @param village
     * @return
     */
    @GetMapping("/getSituationTotal/{village}")
    @Timed
    public String getSituationTotal(@PathVariable String village) {

        return this.grassAuthenticRightService.getSituationTotal(village);
    }


}
